"""
expanding demo

"""
import pandas as pd
import numpy as np


def split_test():
    df = pd.DataFrame(data={'Case': ['A', 'A', 'A', 'B', 'A', 'A', 'B', 'A', 'A'],
                            'Data': np.random.randn(9)})
    print("df:\n", df)
    print("case b:\n", (1*(df['Case']=='B')).cumsum())
    median = (1 * (df['Case'] == 'B')).cumsum().rolling(window=3, min_periods=1).median()
    print("median:\n", median)
    print(*df.groupby(median))
    dfs = list(zip(*df.groupby(median)))
    print(dfs[0])

def pivot_test():
    df = pd.DataFrame(data={'Province': ['ON', 'QC', 'BC', 'AL', 'AL', 'MN', 'ON'],
                            'City': ['Toronto', 'Montreal', 'Vancouver', 'Calgary', 'Edmonton', 'Winnipeg', 'Windsor'],
                            'Sales': [13, 6, 16, 8, 4, 3, 1]})
    print("df:\n", df)
    table = pd.pivot_table(df, values=['Sales'], index=['Province'], columns=['City'], aggfunc=np.sum, margins=True)
    t = table.stack('City')
    print("table:\n", table)
    print("t:\n", t)


if __name__ == '__main__':
    # split_test()
    pivot_test()
